package com.item.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.base.action.CoreController;
import com.base.dao.model.Grid;
import com.base.dao.model.Ret;
import com.base.dialect.PaginationSupport;
import com.base.util.JSONUtils;
import com.base.web.annotation.LoginFilter;
import com.item.dao.model.User;
import com.item.dao.model.UserExample;
import com.item.daoEx.model.UserEx;
import com.item.service.MessageService;
import com.item.service.UserService;
import com.mdx.mobile.commons.verfy.Md5;
import com.zch.dao.model.ChatDeptUser;
import com.zch.dao.model.ChatDeptUserExample;
import com.zch.dao.model.Dept;
import com.zch.dao.model.DeptExample;
import com.zch.dao.model.Meeting;
import com.zch.dao.model.MeetingExample;
import com.zch.dao.model.MeetingJoinUser;
import com.zch.dao.model.MeetingJoinUserExample;
import com.zch.dao.model.Xqmessage;
import com.zch.daoEx.model.MeetingJoinUserEx;
import com.zch.service.ChatDeptUserService;
import com.zch.service.DeptService;
import com.zch.service.MeetingJoinUserService;
import com.zch.service.MeetingService;

@RequestMapping("user")
@Controller
@LoginFilter
public class UserController extends CoreController{
    @Autowired
    private UserService userService;
    @Autowired
    private ChatDeptUserService deptUserService;
    @Autowired
    private DeptService deptService;
    @Autowired
    private MeetingJoinUserService meetingJoinUserService;
    @Autowired
    private MessageService messageService;
    @Autowired
    private MeetingService meetingService;
    @RequestMapping("list")
    @ResponseBody
    public String selectUserList(String name,Integer page,Integer rows,Integer type) throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("key", name);
        if (type != null)
            map.put("type", type);
        PaginationSupport.byPage(page, rows);
        List<UserEx> list= userService.selectList(map);
        return JSONUtils.serialize(new Grid(PaginationSupport.getContext().getTotalCount(), list));
    }
    
    @RequestMapping("insideList")
    @ResponseBody
    public String selectNoCommon(String id) throws Exception {
        ChatDeptUserExample userExample = new ChatDeptUserExample();
        if(StringUtils.isNotBlank(id)){
            userExample.createCriteria().andIdNotEqualTo(id);
        }
        List<ChatDeptUser> deptUsers = deptUserService.selectByExample(userExample);
        List<String> users = new ArrayList<String>(deptUsers.size());
        for (ChatDeptUser user : deptUsers){
            users.add(user.getUserId());
        }
        UserExample example = new UserExample();
        if (users.size() > 0){
            example.createCriteria().andTypeNotEqualTo(0).andIsVerifyEqualTo(1).andIdNotIn(users);
        }else {
            example.createCriteria().andTypeNotEqualTo(0).andIsVerifyEqualTo(1);
        }
        example.setOrderByClause("type,create_time desc");
        List<User> list = userService.selectByExample(example);
        for (User user : list){
            if (user.getType() == 2){
                user.setNickName(user.getFirmName()+"("+user.getNickName()+")-企业");
            }else {
                user.setNickName(user.getNickName()+"("+deptService.getNameByCode(user.getDeptNo())+")-内部");
            }
        }
        return JSONUtils.serialize(list);
    }
    
    @RequestMapping("/findDeptUser")
    @ResponseBody
    public String findDeptUser(String usersId, String actId)throws Exception {
        String[] values = usersId.split(",");
        MeetingJoinUser meetingJoinUser = new MeetingJoinUser();
        MeetingJoinUserExample example = new MeetingJoinUserExample();
        example.createCriteria().andMeetingIdEqualTo(actId);
        meetingJoinUserService.deleteByExample(example);
        
        MeetingExample example2 = new MeetingExample();
        example2.createCriteria().andIdEqualTo(actId);
        List<Meeting> meeting = meetingService.selectByExample(example2);
        String title = meeting.get(0).getTitle();      
        
        for(int i = 0; i < values.length;i++){
            if(values[i].length()==32){
                User user = userService.selectByPrimaryKey(values[i]);
                meetingJoinUser.setId(null);
                meetingJoinUser.setMeetingId(actId);
                meetingJoinUser.setJoinUser(values[i]);
                meetingJoinUser.setJoinUserDeptNo(user.getDeptNo());
                int result = meetingJoinUserService.insert(meetingJoinUser);
                if(result == 1){
                    messageService.findMessage(actId, "您有一个会议要参加", meetingJoinUser.getJoinUser(), 5);
                }
            }     
        }
        return JSONUtils.serialize(new Ret(0));
    }
    
    @RequestMapping("/update")
    @ResponseBody
    public String update(User user){
        if(!StringUtils.isBlank(user.getPassword()))
            user.setPassword(Md5.mD5(user.getPassword()));
        this.userService.updateByPrimaryKeySelective(user);
        return JSONUtils.serialize(new Ret(0));
    }
    
    @RequestMapping("/checkExistMeetingUser")
    @ResponseBody
    public String checkExistMeetingUser(String meetingId)throws Exception {
        MeetingJoinUserExample emample = new MeetingJoinUserExample();
        emample.createCriteria().andMeetingIdEqualTo(meetingId);
        List<MeetingJoinUser> lists = meetingJoinUserService.selectByExample(emample); 
        List< String> list2 = new ArrayList<String>();
        for(MeetingJoinUser ex : lists){
            list2.add(ex.getJoinUser());
        }
        return JSONUtils.serialize(list2);
    }
}
